package siedler.GUI.gameElements;

import java.awt.Color;
import java.awt.Graphics;
import java.io.Serializable;
import java.util.ArrayList;

import siedler.GUI.interfaces.Klickable;
import siedler.GUI.interfaces.Paintable;
import siedler.GUI.other.Coordinate;
import siedler.gaming.User;
import util.simpleIO.Out;

public class Corner implements Paintable, Klickable, Serializable{

	//-----------------------------------------------------------------
	// --- Graphic variables ------------------------------------------
	//
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	//private final static int   x6E  = SiedlerGamePanel.x6E;		// number of edges/connection
	//private final static int   xAdd = SiedlerGamePanel.xAdd;	// value to add before modulo for index <= 0
	private Coordinate klickCoord = zeroCoord;
	private Coordinate position   = zeroCoord;
	
	protected int nullId = 0;									// ID used if only one field is set. (1...6). 0 equals not set.
	ArrayList<Field> fields = new ArrayList<Field>(3);			// connected fields
	ArrayList<Connection> connections = new ArrayList<Connection>(3);	// connected connections
	
	//-----------------------------------------------------------------
	// --- Game specific stuff ------------------------------------
	//
	User owner = null;
	int  state = 0;
	
	public boolean buildSomething(User owner){
		
		// error if a city is already build or something is build by an other user.
		boolean err = state>=2 || (this.owner!=null && this.owner!=owner);
		
		state++;				// build town/city
		this.owner = owner;		// set owner
		
		return err;
	}
	
	//-----------------------------------------------------------------
	// --- Initialization ---------------------------------------------
	//
	
	/**
	 * Constructor
	 */
	public Corner(ArrayList<Field> fields, ArrayList<Connection> connections, int nullId){
		this.fields = fields;
		this.connections = connections;
		
		if(fields.size()<=1) {
			this.nullId = nullId;
		}
	}
	
	/**
	 * initGraphics
	 * Initialize the coordinates of the connection.
	 *  -> Position
	 *  -> Klick Area
	 */
	protected void initGraphics(){
		
		Field f = fields.get(0);						// get first field
		int cornerNumber = f.getCornerNumber(this);		// get field specific corner number
		@SuppressWarnings("static-access")
		int fieldSize = f.getFieldSize();
		
		position   = f.getCornerPosition( cornerNumber );					// get corner position
		klickCoord = new Coordinate(position.x1, position.y1, fieldSize/6);	// area around position
	}
	
	//-----------------------------------------------------------------
	// --- Object specific methods ------------------------------------
	//
	
	
	
	//-----------------------------------------------------------------
	// --- Painting ---------------------------------------------------
	//
	
	@Override
	public void paint(Graphics g) {
		g.setColor(Color.BLACK);
		
		int size  = klickCoord.getWidth(1);	// size for city
		int size2 = size/2;					// half size
		int size3 = size/5;					// size for inner cross and round edges
		
		switch(state){
		
		case 0: 		// nothing
				break;
		
		case 1: 		// town
				g.drawRoundRect(position.x1-size2, position.y1-size2, size, size, size3, size3); 
				break;
		
		case 2:			// city
				g.drawRoundRect(position.x1-size2, position.y1-size2, size, size, size3, size3);
				g.drawRoundRect(position.x1-size2+2, position.y1-size2+2, size-4, size-4, size3, size3);
				Field.drawX(g, position, size3);
				break;
		}
		//g.drawRect(klickCoord.x1, klickCoord.y1, klickCoord.getWidth(1), klickCoord.getHeight(1));
	}
	
	
	//-----------------------------------------------------------------
	// --- Klickable methods ------------------------------------------
	//
	
	@Override
	public Coordinate getPosition(){
		return position;
	}
	@Override
	public Coordinate getKlickArea(){
		return klickCoord;
	}
	@Override
	public boolean isKlickedBy(int x, int y) {
		return klickCoord.contains(x, y);
	}
	@Override
	public boolean klickIt() {
		Out.pl("Klicked: " + this + " ("+this.position+")");
		
		if(state<2){
			buildSomething(new User());
			return true;
		}
		
		return false;
	}
	
	//-----------------------------------------------------------------
	// --- Other methods ----------------------------------------------
	//
	
	/**
	 * Overwritten equals method from object.
	 * TODO: 1 field, 2 corners
	 */
	public boolean equals(Object o){
		
		if(o instanceof Corner){			// test if it is an Connection
			Corner c = (Corner) o;			// cast to connection
			boolean equals = true;			// init
			
			equals = equals && fields.size()==c.fields.size();		// size must be equal
			
			if(fields.size()==1){									// if size==1
				Field f = fields.get(0);							//	field[0] must be equal/contained
				equals = equals && c.fields.contains(f);			//	-
				boolean tmp;										// nullIDs must be equal (zero is joker)
				tmp = nullId==0 || c.nullId==0 || nullId==c.nullId;	// -
				equals = equals && tmp;								// -
				
			}
			else{
				for(Field f : fields){								// if size >1
					equals = equals && c.fields.contains(f);		// -
					if(!equals) break;								// all fields must be equal/contained
				}
			}
			return equals;			// result if objects are of the same type
		}
		return false;			// false if object is of other type
	}
	
	@Override
	public String toString(){
		
		String s = "";
		
		for(int i=0; i<fields.size(); i++){
			s += fields.get(i).toString();
			if(i<fields.size()-1)
				s += ",";
		}
		
		String nId = "";
		if(fields.size()==1)
			nId="#"+nullId;
		
		return "<"+s+nId+">";
	}

}
